package com.web.cms.ad.util;

import com.blue.core.dict.AdvType;
import com.blue.integration.serve.Bucket;
import com.blue.integration.serve.StorageClient;
import com.blue.jdbc.Page;
import com.web.cms.ad.message.AdvRequest;
import com.web.cms.ad.message.AdvResponse;
import com.web.cms.ad.model.Adv;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 广告转换器
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2017年11月13日
 */
public class AdvConvertor
{
	private AdvConvertor()
	{
	}
	
	public static Adv toAdv(AdvRequest request)
	{
		if (request == null)
			return null;
		
		Adv adv = new Adv();
		adv.setId(request.getId());
		adv.setWebId(request.getWebId());
		adv.setPosId(request.getPosId());
		adv.setName(request.getName());
		adv.setContent(request.getContent());
		adv.setUrl(request.getUrl());
		adv.setDescp(request.getDescp());
		adv.setStartTime(request.getStartTime());
		adv.setEndTime(request.getEndTime());
		adv.setCreateTime(request.getCreateTime());
		adv.setClicks(request.getClicks());
		adv.setState(request.getState());
		adv.setSort(request.getSort());
		return adv;
	}
	
	public static List<Adv> toAdv(List<AdvRequest> requestList)
	{
		List<Adv> list = new ArrayList<>();
		if (requestList == null || requestList.isEmpty())
			return list;
		
		for (AdvRequest request : requestList)
		{
			list.add(toAdv(request));
		}
		return list;
	}
	
	public static AdvResponse toResponse(Adv adv, StorageClient storageClient)
	{
		if (adv == null)
			return null;
		
		AdvResponse response = new AdvResponse();
		response.setId(adv.getId());
		response.setWebId(adv.getWebId());
		response.setPosId(adv.getPosId());
		response.setName(adv.getName());
		response.setContent(adv.getContent());
		response.setUrl(adv.getUrl());
		response.setDescp(adv.getDescp());
		response.setStartTime(adv.getStartTime());
		response.setEndTime(adv.getEndTime());
		response.setCreateTime(adv.getCreateTime());
		response.setClicks(adv.getClicks());
		response.setState(adv.getState());
		response.setSort(adv.getSort());

		response.setPosCode(adv.getPosCode());
		response.setPosName(adv.getPosName());
		response.setPosType(adv.getPosType());
		response.setWebCode(adv.getWebCode());

		if (response.getPosType() == AdvType.PIC)
		{
			response.setPicUrl(storageClient.getUrl(Bucket.CMS, response.getContent()));
		}

		return response;
	}
	
	public static List<AdvResponse> toResponse(List<Adv> list, StorageClient storageClient)
	{
		List<AdvResponse> responseList = new ArrayList<>();
		if (list == null || list.isEmpty())
			return responseList;
		
		for (Adv adv : list)
		{
			responseList.add(toResponse(adv, storageClient));
		}
		return responseList;
	}
	
	public static Map<Integer, AdvResponse> toResponse(Map<Integer, Adv> map, StorageClient storageClient)
	{
		Map<Integer, AdvResponse> responseMap = new HashMap<>();
		if (map == null || map.isEmpty())
			return responseMap;
		
		for (Map.Entry<Integer, Adv> entry : map.entrySet())
		{
			responseMap.put(entry.getKey(), toResponse(entry.getValue(), storageClient));
		}
		return responseMap;
	}
	
	public static Page<AdvResponse> toResponse(Page<Adv> page, StorageClient storageClient)
	{
		if (page == null)
			return null;
		
		Page<AdvResponse> responsePage = new Page<>();
		responsePage.setCurPage(page.getCurPage());
		responsePage.setRows(page.getRows());
		responsePage.setSize(page.getSize());
		responsePage.setStartRowNo(page.getStartRowNo());
		responsePage.setTotalPage(page.getTotalPage());
		responsePage.setObjectList(toResponse(page.getObjectList(), storageClient));
		return responsePage;
	}
	
}
